﻿@model PagedList.IPagedList<Models.DataModel.ContributionRate>
@using PagedList.Mvc;
@{
    ViewBag.Title = "Contribution Rate";
    Layout = "~/Views/Shared/_Layout.cshtml";
    List<Models.DataStore.ContributionRateExtra> ListRate = ViewBag.ListContributionRate;
}
<link href="~/Content/bootstrap.css" rel="stylesheet" />

    <h2>List ContributionRate</h2>
    <p>
        @using (Html.BeginForm("Index", "ContributionRate", FormMethod.Get))
        {
            <div class="form-inline">
                <div class="form-group">
                    <input type="text" class="form-control" id="searchtext"  placeholder="Please input value" value="@ViewBag.SearchText">
                </div>
                <button type="button" onclick="search();" class="btn btn-default">Search</button>
                <button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Create", "ContributionRate")'">Add New ContributionRate</button>
            </div>  
    }
        <br />
    <div class="table-responsive" id="list">
        <table class="table table-bordered table-condensed" id="tbcontribution">
            <thead>
                <tr>
                    <th class="form-inline" style="width:60px; ">
                        <input id="ChkCheckFull" name="delall" type="checkbox" value="" />
                        <button type="submit" class="glyphicon glyphicon-trash btn-sm" value="" onclick="return DeleteContributionRateSelected()" style="color:red; border:none; background:none"></button>
                    </th>
                    <th>
                        @Html.Label("Year")
                    </th>
                    <th>
                        @Html.Label("Fund Code")
                    </th>
                    <th>
                        @Html.Label("LaborType")
                    </th>
                    <th>
                        @Html.Label("Port Name")
                    </th>
                    <th>
                        @Html.Label("Port Number")
                    </th>
                    <th>
                        @Html.Label("Rate")
                    </th>
                    <th>
                        @Html.Label("Percentage")
                    </th>
                    <th>
                        @Html.Label("CreatedOn")
                    </th>
                    <th>
                        @Html.Label("ModifiedOn")
                    </th>
                    <th>
                        @Html.Label("Option")

                    </th>
                </tr>
            </thead>
            <tbody class="tbody" id="tbody"></tbody>
        </table>
    </div>
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("Index",
              new { page, searchtext = ViewBag.SearchText }))

    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                url: "/ContributionRate/NameFund",
                type: "GET",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    var option = {
                        source: result
                    };
                    $('#searchtext').typeahead(option);
                }
            });
            _getAll();
        });
        function _getAll(searchtext) {
            var i = 0;
            $.ajax({
                url: "/ContributionRate/GetAll",
                type: "GET",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ searchtext: searchtext }),
                dataType: "json",
                success: function (result) {
                    var html = '';
                    $.each(result, function (key, item) {
                        ++i;
                        var style = i % 2 == 0 ? "even" : "odd";
                        html += '<tr class=' + style + '>';
                        html += '<td> <input type="checkbox" class="checkCust" name="ContributionRateId" id="' + item.ContributionRateId + '" value=' + item.ContributionRateId + ' /> </td>'
                        html += '<td>' + item.Year + '</td>';
                        html += '<td class="fcode">' + item.FundCode + '</td>';
                        html += '<td>' + item.LaborType + '</td>';
                        html += '<td>' + item.PortName + '</td>';
                        html += '<td>' + item.PortNumber + '</td>';
                        html += '<td>' + item.Rate + '</td>';
                        html += '<td>' + item.Percentage + '</td>';
                        html += '<td>' + FormatJsonDate(item.CreatedOn) + '</td>';
                        html += '<td>' + FormatJsonDate(item.ModifiedOn) + '</td>';
                        html += '<td> <a href="/ContributionRate/Edit/' + item.ContributionRateId + '">Edit</a> |<a href="javascript:DeleteContributionRate(\'' + item.ContributionRateId + '\');">Delete</a></td>';
                        html += '</tr>';
                    });
                    $('#list tbody').html(html);
                },
                error: function (errormessage) {
                    alert(errormessage.responseText);
                }
            });
        }
        $("#ChkCheckFull").click(function () {
            if ($("#ChkCheckFull").is(':checked') == true) {
                $(".checkCust").prop('checked', true);
            }
            else {
                $(".checkCust").prop('checked', false);
            }
        });

        function DeleteContributionRate(ContributionRateId) {
            var cf = confirm('Are you sure you want to delete this row?');
            if (cf) {
                $.ajax({
                    url: "/ContributionRate/Delete/" + ContributionRateId,
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    async: true,
                    success: function (result) {
                        _getAll();
                    },
                    error: function (errormessage) {
                        alert(errormessage.responseText);
                    }
                });
            }
        }
        // conver datetime
        function FormatJsonDate(jsonDt) {
            var MIN_DATE = -62135578800000; // const

            var date = new Date(parseInt(jsonDt.substr(6, jsonDt.length - 8)));
            return date.toString() == new Date(MIN_DATE).toString() ? "" : (date.getMonth() + 1) + "\/" + date.getDate() + "\/" + date.getFullYear();
        }

        /// xoa nhieu dong
        function DeleteContributionRateSelected() {
            var cf = confirm('Are you sure you want to delete rows selected?');
            var lstData = [];
            if (cf == true) {
                $('.checkCust').each(function () {
                    var sThisVal = (this.checked ? "1" : "0");
                    if (sThisVal == 1) {
                        lstData.push($(this).attr('id'));
                    }
                });

                // call controller and tranfer data
                $.ajax({
                    url: "/ContributionRate/DeleteSelectedContributionRate/" + lstData,
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    async: true,
                    data: JSON.stringify({ countryItemDelete: lstData }),
                    success: function (result) {
                        _getAll();
                    }
                });
            }
        }
       
        $("#searchtext").change(function search() {
            //do whatever you need to do on actual change of the value of the input field
            var table = $("table tbody");

            table.find('tr').each(function (i, el) {
                var $tds = $(this).find('td'),
                 fundcode = $tds.eq(2).text();
                if(fundcode.search($("#searchtext").val())==-1)
                {
                    $(this).hide();
                  
                }else $(this).show();
            });
        });
        
       
    </script>
